package com.flower8.controller;

import com.flower8.model.Guide;
import com.flower8.service.GuideService;
import com.flower8.sysconfig.constants.MsgConstants;
import com.flower8.utils.PageData;
import com.flower8.utils.ftpUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: tanglin
 * @Date: 2019/10/23 14:00
 * @Version: 1.0
 */
@RestController
@RequestMapping("/guide")
public class GuideController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(GuideController.class);
    
    @Autowired
    private GuideService guideService;

    /**
     * 查询引导页列表
     * @param request
     * @return
     */
    @RequestMapping("/getGuideList")
    public Object getGuideList(HttpServletRequest request){
        logBefore(log, "++++++++++ 查询引导页列表 ++++++++++");
        PageData pd = new PageData(request);

        pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
        pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

        Map res = new HashMap();
        try {
            List<Guide> guideList =  guideService.getGuideList(pd);
            int total = guideService.getGuideListCount(pd);
            res.put("data", guideList);
            res.put("recordsTotal", total);   //dataTable分页需要
            res.put("recordsFiltered", total);//dataTable分页需要
            res.put("draw", pd.get("draw").toString());//dataTable分页需要
            logAfter(log);
            Map<String, Object> dataMap = (Map<String, Object>) responseResultForSuccess(res);
            // 兼容旧网页
            dataMap.put("recordsTotal", total);   //dataTable分页需要
            dataMap.put("recordsFiltered", total);//dataTable分页需要
            dataMap.put("draw", pd.get("draw").toString());//dataTable分页需要
            return dataMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     *  根据引导页的id查询
     * @param request
     * @return
     */
    @RequestMapping("/getGuideById")
    public Object getGuideById(HttpServletRequest request){
        logBefore(log, "++++++++++  根据引导页的id查询 ++++++++++");
        PageData pd = new PageData(request);

        Map res = new HashMap();
        try {
            Guide guide =  guideService.getGuideById(pd);
            res.put("guide", guide);
            logAfter(log);
            return responseResultForSuccess(res);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 更新/新增引导页
     * @param guide
     * @return
     */
    @RequestMapping("/updateGuide")
    public Object updateGuide(@RequestBody Guide guide){
        try {
            if(guide.getId() != null){
                logBefore(log, "++++++++++ 更新引导页接口 ++++++++++");
                guideService.updateGuide(guide);
            }else{
                logBefore(log, "++++++++++ 新增引导页接口 ++++++++++");
                guideService.addGuide(guide);
            }

            logAfter(log);
            return responseResultForSuccess(null, "操作成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("操作失败");
        }
    }

    /**
     * 推荐图标上传
     * @param pic
     * @return
     */
    @RequestMapping("/uploadFile")
    public Object uploadFile(@RequestParam MultipartFile pic){
        logBefore(log, "++++++++++ 推荐图标上传 ++++++++++");
        try {
            String filePath = "";
            filePath = ftpUtil.uploadFile(pic,2);//主要就是这里实现了ftp的文件上传
            if (!"".equals(filePath)) {
                log.info("上传文件成功!");
                logAfter(log);
                Map<String, Object> data = new HashMap<>();
                data.put("url", filePath);
                return responseResultForSuccess(data);
            }else{
                return responseResultForError("上传图片失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("上传图片失败");
        }
    }

    /**
     * 删除引导页
     * @param id
     * @return
     */
    @RequestMapping("/deleteGuide")
    public Object deleteGuide(@RequestParam long id){
        logBefore(log, "++++++++++ 删除引导页 ++++++++++");
        try {
            guideService.deleteGuide(id);
            logAfter(log);
            return responseResultForSuccess(null, "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("删除失败");
        }
    }
}
